
package br.com.clicarros.controle.anuncio;

import br.com.clicarros.dominio.anuncio.Anuncio;
import br.com.clicarros.dominio.anuncio.Imagem;
import br.com.clicarros.fachada.IFachada;
import br.com.clicarros.fachada.implementacao.Fachada;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.tomcat.util.http.fileupload.FileItem;
import org.apache.tomcat.util.http.fileupload.FileItemFactory;
import org.apache.tomcat.util.http.fileupload.FileUploadException;
import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory;
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;

/**
 *
 * @author Bruno
 */
@WebServlet(name = "UploadImagem", urlPatterns = {"/UploadImagem"})
public class UploadImagem extends HttpServlet {
IFachada fachada = new Fachada();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        Imagem i = new Imagem();
        
        i = uploadImagem(request);
        
        System.out.println(i.getCaminho());
        
        fachada.inserir(i);
        
        request.setAttribute("anuncios", fachada.listarTodos(new Anuncio()));
        request.getRequestDispatcher("Administrador/anuncio/anuncios.jsp").forward(request,response);
        
      
 
   }
  


private Imagem uploadImagem(HttpServletRequest request){

 Imagem i = new Imagem();
 Anuncio a = new Anuncio();
 i.setAnuncio(a);
    
  FileItemFactory factory = new DiskFileItemFactory();
  ServletFileUpload upload = new ServletFileUpload(factory);
  List items = null;

  try {
  items = upload.parseRequest(request);
  } catch (FileUploadException e) {
    e.printStackTrace();
  }
  
  Iterator itr = items.iterator();
  while (itr.hasNext()) {
        FileItem item = (FileItem) itr.next();
  if (item.isFormField()){
      
      if(item.getFieldName().equals("id")){

        i.getAnuncio().setIdanuncio(Integer.parseInt(item.getString()));
      }

  } else {
  try {
  String itemName = item.getName();
  Random generator = new Random();
  int r = Math.abs(generator.nextInt());

  String reg = "[.*]";
  String replacingtext = "";

  Pattern pattern = Pattern.compile(reg);
  Matcher matcher = pattern.matcher(itemName);
  StringBuffer buffer = new StringBuffer();

  while (matcher.find()) {
  matcher.appendReplacement(buffer, replacingtext);
  }
  int IndexOf = itemName.indexOf("."); 
  String domainName = itemName.substring(IndexOf);

  String finalimage = buffer.toString()+"_"+r+domainName;

    i.setCaminho("imagens/anuncios/"+finalimage);
  
  File savedFile = new File("C:\\Users\\Bruno\\Documents\\NetBeansProjects\\Cli-Carros\\web\\imagens\\anuncios\\"+finalimage);
  item.write(savedFile);


        
    }catch (Exception e){

    }
   }
  }

  return i;
  
}

}



